package com.shuoyue.ffmpegvideocut.controller;

import com.shuoyue.ffmpegvideocut.controller.oss.MinioConfig;
import com.shuoyue.ffmpegvideocut.controller.oss.OssUtil;
import com.shuoyue.ffmpegvideocut.controller.oss.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * m3u8管理
 */
@RestController
@RequestMapping("/m3u8")
public class M3u8Controller {
    @Autowired
    private OssUtil minIoUtil;
    @Autowired
    private MinioConfig minioConfig;

    /**
     * @param type     1 :访问keyInfo文件,执行FFmpeg文件时使用;2 访问key文件,执行加密和播放解密时使用
     * @param version  版本号 仅接受数字;minio同步名字:v+数字
     * @param response
     * @return
     */
    @GetMapping("/key/{type}/{version}")
    public ResultEntity download(@PathVariable Integer type, @PathVariable Integer version, HttpServletRequest request, HttpServletResponse response) {
        // TODO 判断request来源,非后台端禁止使用version=1
        if (type == 1) {
            //keyInfo 文件
            minIoUtil.download(minioConfig.getBucketName(), "m3u8key/v" + version + "/enc.keyinfo", response);
        } else if (type == 2) {
            //key文件
            minIoUtil.download(minioConfig.getBucketName(), "m3u8key/v" + version + "/enc.key", response);
        }
        return ResultEntity.success("");
    }
}
